//
//  ElectricityMeterController.m
//  PropertyMananger
//
//  Created by bh on 17/2/23.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "ElectricityMeterController.h"
#import "CheckFormCell.h"
#import "CheckComfirmCell.h"
#import "CheckFromInputCell.h"
#import "AddressView.h"
#import "HouseQueryModel.h"

@interface ElectricityMeterController ()
@property (nonatomic,copy) NSString * adressStr;//选择地址
@property (nonatomic,strong) NSMutableArray * floorArray;//楼栋数据源
@property (nonatomic,copy) NSString * selectedTime;//选择的的时间

@property (nonatomic,copy) NSString * BuildUnitId;//楼栋ID
@property (nonatomic,copy) NSString * roomNum;//房间号
@property (nonatomic,copy) NSString * readingNum;//抄表读数

@end

@implementation ElectricityMeterController
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.adressStr = @"请选择楼栋";
//    
//    [self initTableViewWithView:self.view frame:CGRectMake(0, 50, BHscreenW, BHscreenH) isGroup:NO];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.backgroundColor = BHGrayColor;
//    
//    self.floorArray = [[NSMutableArray alloc]init];
//    
//    [self getYearAndMonthData];
    
        UIView * lineView = [[UIView alloc]init];
        [self.view addSubview:lineView];
        lineView.backgroundColor = [BHColor(153, 153, 153)colorWithAlphaComponent:0.2];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(44);
            make.size.mas_equalTo(CGSizeMake(BHscreenW, 15));
        }];

//    [MBProgressHUD showError:@"正在努力开发中" toView:self.view time:1.5];
    
}

#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 4;
    }else{
        return 1;
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            CheckFormCell * cell = [CheckFormCell cellWithTableView:tableView];
            cell.iconImg.image = IMG(@"ico_meter_reading_ban");
            cell.titleLabel.text = @"楼栋";
            cell.subTitle.text = self.adressStr;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            return cell;
            
        }else if (indexPath.row == 1){
            
            CheckFromInputCell * cell = [CheckFromInputCell cellWithTableView:tableView];
            
            cell.titleLabel.text = @"房号";
            cell.inputTF.placeholder = @"请输入房间号";
            cell.iconImg.image = IMG(@"ico_meter_reading_room");
            self.roomNum = cell.inputTF.text;
            return cell;
            
        }else if (indexPath.row == 2){
            
            CheckFromInputCell * cell = [CheckFromInputCell cellWithTableView:tableView];
            cell.iconImg.image = IMG(@"ico_meter_reading_reading");
            cell.titleLabel.text = @"读数";
            cell.inputTF.placeholder = @"请输入抄表读数";
            
            self.readingNum = cell.inputTF.text;
            
            return cell;
            
        }else if (indexPath.row == 3){
            
            CheckFormCell * cell = [CheckFormCell cellWithTableView:tableView];
            cell.titleLabel.text = @"日期";
            cell.subTitle.text = self.selectedTime;
            cell.subTitle.textColor = BHColor(102, 102, 102);
            cell.iconImg.image = IMG(@"ico_meter_reading_date");
            return cell;
            
        }
        
    }else if (indexPath.section == 1){
        
        CheckComfirmCell * cell = [CheckComfirmCell cellWithTableView:tableView];
        [cell.comfirmBtn addTarget:self action:@selector(queryClick:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        
        return 50;
    }else{
        return 170;
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 15;
    }else{
        
        return 0.001f;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            {
                AddressView *addressView = [[AddressView alloc] initWithFrame:CGRectMake(0, 0, [UIApplication sharedApplication].keyWindow.frame.size.width, [UIApplication sharedApplication].keyWindow.frame.size.height)];
                addressView.block = ^(NSString *addressStr){
                    
                    DLog(@"选择了 === %@",addressStr);
                    self.adressStr = addressStr;
                    
                    [self.tableView reloadData];
                    
                };
                
                addressView.BuildUnitIDBlock = ^(NSString * BuildUnitId){
                    
                    DLog(@"BuildUnitId===%@",BuildUnitId);
                    self.BuildUnitId = BuildUnitId;
                };
                
                [addressView showView:self.view];
                
            }
        }
    }
    
}
#pragma mark - 点击查询
- (void)queryClick:(UIButton *)button{
    
    DLog(@"点击提交");
    
    [self postSaveReadingData];
}
- (void)postSaveReadingData{
    
    NSDictionary * parms = @{
                             @"Cid":userModel.CommunityID,
                             @"BuildUnitId":self.BuildUnitId,
                             @"MeterReadingType":@"2",
                             @"CurrentMeterReading":self.readingNum,
                             @"MeterReadingMan":userModel.RealName,
                             @"RoomName":self.roomNum,
                             @"MeterReadingTime":self.selectedTime,
                             @"json":@"\{\"jsonEntity\":\{},\"action\":\"savereading\",\"version\":\"V1.0\"}"};
    [HomeNetWorkRequest postSaveReadingQuerySuccess:^(id data) {
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"提交抄表查询数据 == %@",array);
        DLog(@"%@",dict[@"Msg"]);
        for (NSDictionary * dict in array) {
            
            HouseQueryModel * model = [[HouseQueryModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dict];
            
            [self.floorArray addObject:model];
        }
        
//        NSDictionary * floorDataDic  = @{@"key":self.floorArray};
//        DLog(@"%@",floorDataDic);
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"GetFloorNotification" object:nil userInfo:floorDataDic];
        
        [self.tableView reloadData];
    } Fail:^(NSString *errorDes) {
        
    } param:parms];
    
    
    
}

#pragma mark - 获取系统时间
- (void)getYearAndMonthData{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSString * timeDataStr = [NSString stringWithFormat:@"%@",dateNow];
    DLog(@" === %@",dateNow);
    
    NSArray * arr = [timeDataStr componentsSeparatedByString:@" "];
    DLog(@"日期格式化 == %@",arr[0]);
    
    NSArray * timeArr = [timeDataStr componentsSeparatedByString:@"-"];
    NSString * timeStr = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
    DLog(@"拼接好的时间===%@",timeStr);
    self.selectedTime = arr[0];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}


@end
